

(function () {
    var position='absolute';
    var elements=[];
    function Food(options){
        options=options||{};
        this.x=options.x||0;
        this.y=options.y||0;
        this.width=options.width||20;
        this.height=options.height||20;
        this.backgroundColor=options.backgroundColor||'green';


    }
//渲染
    Food.prototype.render=function (map) {
//删除食物
        remove();

        // console.log(map.offsetWidth);//map.width  拿不到
        this.x=Tools.getRandom(0,map.offsetWidth/this.width-1)*this.width;
        this.y=Tools.getRandom(0,map.offsetHeight/this.height-1)*this.height;
        // console.log(this.x);
        // console.log(this.y);

        var div=document.createElement('div');
        div.style.left=this.x+'px';
        div.style.top=this.y+'px';
        div.style.width=this.width+'px';
        div.style.height=this.height+'px';
        div.style.backgroundColor=this.backgroundColor;
        div.style.position=position;
        map.appendChild(div);
        //删除食物

        elements.push(div);
        function remove(){
            for(var i=elements.length-1;i>=0;i--){
                elements[i].parentNode.removeChild(elements[i]);
                elements.splice(i,1);
            }
        }

    };
    window.Food=Food;
})();

